home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / grafica / ghostscript / 5.10 / data / viewjpeg.ps < prev    next >
Text File  |  1997-05-20  |  4KB  |  145 lines

  1. %! viewjpeg.ps   Copyright (C) Thomas Merz 1994
  2. %
  3. % View JPEG files with Ghostscript
  4. %
  5. % This PostScript code relies on level 2 features.
  6. %
  7. % Only JPEG baseline, extended sequential, and progressive files
  8. % are supported.  Note that Adobe PostScript level 2 does not include
  9. % progressive-JPEG support.  Ghostscript with IJG JPEG v6 or later
  10. % will decode progressive JPEG, but only if you edit gsjmorec.h to
  11. % enable that feature.
  12. %
  13. % Author's address:
  14. % ------------------------------+
  15. % {(pstack exec quit) = flush } |    Thomas Merz, Munich
  16. % pstack exec quit              |    voice +49/89/29160728
  17. % ------------------------------+    tm@muc.de  http://www.muc.de/~tm/
  18. %
  19. % Updated by L. Peter Deutsch 20-May-1997:
  20. %   move the usage example to the beginning
  21. % Updates by Tom Lane 6-Sep-1995
  22.  
  23. % Usage example:
  24. %    (jpeg-6/testimg.jpg) viewJPEG
  25.  
  26. /languagelevel where {pop languagelevel 2 lt}{true} ifelse {
  27.   (JPEG needs PostScript Level 2!\n) print flush stop
  28. } if
  29.  
  30. /JPEGdict 20 dict def
  31. JPEGdict begin
  32.  
  33. /NoParamMarkers [    % JPEG markers without additional parameters
  34.     16#D0 16#D1 16#D2 16#D3 16#D4 16#D5 16#D6 16#D7 16#D8 16#01
  35. ] def
  36.  
  37. /NotSupportedMarkers [     % JPEG markers not supported by PostScript level 2
  38.     16#C3 16#C5 16#C6 16#C7 16#C8 16#C9 16#CA 16#CB 16#CD 16#CE 16#CF
  39. ] def
  40.  
  41. % Names of color spaces
  42. /ColorSpaceNames << /1 /DeviceGray /3 /DeviceRGB /4 /DeviceCMYK >> def
  43.  
  44. % read one byte from file F
  45. % - ==> int --or-- stop context
  46. /NextByte { 
  47.     F read not { (Read error in ViewJPEG!\n) print flush stop } if
  48. } bind def
  49.  
  50. /SkipSegment {    % read two bytes and skip that much data
  51.     NextByte 8 bitshift NextByte add 2 sub { NextByte pop } repeat
  52. } bind def
  53.  
  54. % read width, height, and # of components from JPEG markers
  55. % and store in dict
  56. /readJPEGmarkers {    % - ==> dict --or-- stop context
  57.     5 dict begin
  58.  
  59.     { % loop: read JPEG marker segments until we find SOFn marker or EOF
  60.     NextByte
  61.     16#FF eq {                % found marker
  62.         /markertype NextByte def
  63.         % Is it S0F0=baseline, SOF1=extended sequential, SOF2=progressive ?
  64.         markertype dup 16#C0 ge exch 16#C2 le and {
  65.         NextByte pop NextByte pop    % segment length
  66.         % Ghostscript and Adobe PS accept only data precision 8
  67.         NextByte 8 ne {
  68.             (Error: not 8 bits per component!\n) print flush stop 
  69.         } if
  70.  
  71.         % Read crucial image parameters
  72.         /height NextByte 8 bitshift NextByte add def
  73.         /width NextByte 8 bitshift NextByte add def
  74.         /colors NextByte def
  75.  
  76.         DEBUG { currentdict { exch == == } forall flush } if
  77.         exit
  78.         } if
  79.  
  80.         % detect several segment types which are not compatible with PS
  81.         NotSupportedMarkers {
  82.         markertype eq {
  83.             (Marker ) print markertype == 
  84.             (not supported!\n) print flush stop
  85.         } if 
  86.         } forall 
  87.  
  88.         % Skip segment if marker has parameters associated with it
  89.         true NoParamMarkers { markertype eq {pop false exit} if } forall 
  90.         { SkipSegment } if
  91.     } if
  92.     } loop
  93.  
  94.     currentdict dup /markertype undef
  95.     end
  96. } bind def
  97.  
  98. end    % JPEGdict
  99.  
  100. % read image parameters from JPEG file and display the image
  101. /viewJPEG {        % <file|string> ==> -
  102.     save 
  103.     JPEGdict begin
  104.     /saved exch def
  105.     /scratch 1 string def
  106.     dup type /stringtype eq { (r) file } if
  107.     /F exch def
  108.  
  109.     readJPEGmarkers begin
  110.     F 0 setfileposition        % reset file pointer
  111.  
  112.     % We use the whole clipping area for the image (at least in one dimension)
  113.     gsave clippath pathbbox grestore
  114.     /ury exch def /urx exch def
  115.     /lly exch def /llx exch def
  116.  
  117.     llx lly translate
  118.     width height scale
  119.  
  120.     % use whole width or height, whichever is appropriate
  121.     urx llx sub width div ury lly sub height div
  122.     2 copy gt { exch } if pop        % min
  123.     dup scale
  124.     ColorSpaceNames colors scratch cvs get setcolorspace
  125.  
  126.     % prepare image dictionary
  127.     << /ImageType 1
  128.        /Width width
  129.        /Height height
  130.        /ImageMatrix [ width 0 0 height neg 0 height ]
  131.        /BitsPerComponent 8
  132.        % If 4-component (CMYK), assume data is inverted per Adobe Photoshop
  133.        colors 4 eq {
  134.          /Decode [ colors { 1 0 } repeat ]
  135.        } {
  136.          /Decode [ colors { 0 1 } repeat ]
  137.        } ifelse
  138.        /DataSource F /DCTDecode filter
  139.     >> image
  140.  
  141.     end        % image parameter dictionary
  142.  
  143.     saved end restore
  144. } bind def
  145.